import { BaseWindow } from "../../../component/window/BaseWindow";
import { Language } from "../../../../extend/language/Language";
import { DropItemInfo } from "../../scene/player/info/DropItemInfo";
import { PageTool } from "../../../common/global/PageTool";
import { DropItemPlayer } from "../../scene/player/item/DropItemPlayer";
import { ResouceConst } from "../../../component/gconst/ResouceConst";
import { GList } from "../../../../com/mui/controls/GList";
import { DropItemRenderer } from "./DropItemRenderer";
import { ScrollPolicy } from "../../../../fl/controls/ScrollPolicy";
import { ListEvent } from "../../../../fl/events/ListEvent";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { DropItemData } from "./DropItemData";
import { GButton } from "../../../../com/mui/controls/GButton";
import { TextBox } from "../pack/TextBox";
import { ItemConfig } from "../../resource/ItemConfig";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { LoaderPriority } from "../../../../com/gengine/resource/LoaderPriority";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { FlyToNavbarTool } from "../award/FlyToNavbarTool";
type int = number;
//class DropItemWindow
    
    export  class DropItemWindow extends BaseWindow
    {
        constructor()
        {
            super();
            setSize(230, 303);
            title = Language.getString(90288);
            titleHeight = 25;
            return;
        }

        public get dropItemInfo(): DropItemInfo
        {
            if (this._dropItemInfo) 
            {
                return this._dropItemPlayer.dropItemInfo;
            }
            return null;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            this._pageTool = new PageTool(null, 5);
            this.setlistView(20, 36);
            this.setPageView(95, 240);
            this.setPageText();
            return;
        }

        public set dropItemPlayer(arg1: DropItemPlayer)
        {
            this._dropItemPlayer = arg1;
            if (this._dropItemPlayer && this._dropItemPlayer.dropItemInfo) 
            {
                this.showData(this._dropItemPlayer.dropItemInfo.getArray());
            }
            return;
        }

        public showData(arg1: Array<any>): void
        {
            this._pageTool.array = arg1;
            this._list.dataProvider.removeAll();
            this._list.dataProvider.addItems(this._pageTool.getByPageNo(1));
            this.setPageText();
            return;
        }

        /* internal  */setPageText(): void
        {
            this._pageText.label = this._pageTool.currentPage + "/" + this._pageTool.pagesCount;
            return;
        }

        /* internal  */setlistView(arg1: int, arg2: int): void
        {
            var loc1=ResouceConst.getScaleBitmap("WindowCenterB");
            loc1.setSize(190, 257);
            loc1.x = arg1;
            loc1.y = arg2;
            this.addChild(loc1);
            this._list = new GList();
            this._list.styleName = "ScrollBarNormal";
            this._list.setStyle("skin", new Object /* flash.display.Bitmap */());
            this._list.setStyle("cellRenderer", DropItemRenderer);
            this._list.rowCount = 5;
            this._list.rowHeight = 40;
            this._list.setSize(200, 220);
            this._list.verticalScrollPolicy = ScrollPolicy.OFF;
            this._list.move(arg1 + 2, arg2 + 3);
            this._list.addEventListener(ListEvent.ITEM_CLICK, this.onItemClickHandler);
            this._list.dataProvider = new DataProvider();
            this.addChild(this._list);
            return;
        }

        public pickServerAllItem(): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Pick_AllItem, this._pageTool.array));
            return;
        }

        /* internal  */onItemClickHandler(arg1: ListEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Pick_Item, arg1.item));
            return;
        }

        public pickAllItem(): void
        {
            var loc1=this._pageTool.array;
            this.flyAllItemToBackPack(loc1);
            var loc2=this.dropItemInfo;
            if (loc2) 
            {
                loc2.clear();
            }
            this.hide();
            return;
        }

        public autoPickItem(arg1: Map<any, any> /* flash.utils.Dictionary */, arg2: Boolean=true): void
        {
            var loc1=null;
            if (this._dropItemPlayer && this._dropItemPlayer.dropItemInfo) 
            {
                loc1 = this._dropItemPlayer.dropItemInfo.update(arg1);
                this.flyAllItemToBackPack(loc1);
            }
            if (arg2) 
            {
                this.hide();
            }
            return;
        }

        public pickItem(arg1: DropItemData): void
        {
            this.flyItemToBackPack(arg1);
            if (this._dropItemPlayer && this._dropItemPlayer.dropItemInfo) 
            {
                this._dropItemPlayer.dropItemInfo.removeDropItem(arg1);
                if (this._dropItemPlayer.dropItemInfo.getArray().length <= 0) 
                {
                    this.hide();
                }
                else 
                {
                    this.showData(this._dropItemPlayer.dropItemInfo.getArray());
                }
            }
            return;
        }

        /* internal  */setPageView(arg1: int, arg2: int): void
        {
            var loc1=new GButton();
            loc1.name = "PrevPageButton";
            loc1.label = "";
            loc1.styleName = "PrevPageButton";
            loc1.setSize(21, 20);
            loc1.move(arg1 - 20, arg2);
            loc1.addEventListener(flash.events.MouseEvent.CLICK, this.onPreClickHandler);
            this.addChild(loc1);
            this._pageText = new TextBox();
            this._pageText.textField.textColor = 11661311;
            var loc2;
            (loc2 = this._pageText.textField.defaultTextFormat).align = flash.text.TextFormatAlign.CENTER;
            this._pageText.textField.defaultTextFormat = loc2;
            this._pageText.bgWidth = 37;
            this._pageText.bgHeight = 18;
            this._pageText.x = arg1 + 3;
            this._pageText.y = arg2;
            this.addChild(this._pageText);
            var loc3;
            (loc3 = new GButton()).name = "NextPageButton";
            loc3.label = "";
            loc3.styleName = "NextPageButton";
            loc3.setSize(21, 20);
            loc3.move(arg1 + 40, arg2);
            loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onNextClickHandler);
            this.addChild(loc3);
            var loc4;
            (loc4 = new GButton()).addEventListener(flash.events.MouseEvent.CLICK, this.onPickAllHandler);
            loc4.styleName = "Button";
            loc4.label = "全部拾取";
            loc4.setSize(75, 25);
            loc4.move(arg1 - 16, arg2 + 22);
            this.addChild(loc4);
            return;
        }

        /* internal  */onPickAllHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.pickServerAllItem();
            return;
        }

        /* internal  */onNextClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._list.dataProvider.removeAll();
            this._list.dataProvider.addItems(this._pageTool.getNextPage(this._pageTool.currentPage));
            return;
        }

        /* internal  */onPreClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._list.dataProvider.removeAll();
            this._list.dataProvider.addItems(this._pageTool.getPrevPage(this._pageTool.currentPage));
            return;
        }

        /* internal  */getClildPos(arg1: int): Object /* flash.geom.Point */
        {
            return new Object /* flash.geom.Point */(this.x + 20, this.y + 30 * (arg1 + 1));
        }

        /* internal  */flyAllItemToBackPack(arg1: Array<any>): void
        {
            var loc2=null;
            var loc1=0;
            var loc3=0;
            var loc4=arg1;
            for(loc2 of loc4) 
            {
                this.flyItemToBackPack(loc2, loc1);
                ++loc1;
            }
            return;
        }

        /* internal  */flyItemToBackPack(arg1: DropItemData, arg2: int=0): void
        {
            var loc1=null;
            var loc2=null;
            if (arg1) 
            {
                loc1 = ItemConfig.instance.getInfoByCode(arg1.itemCode);
                if (loc1) 
                {
                    loc2 = loc1.url;
                    if (loc1.item.category == ECategory._ECategoryProp && loc1.item.type == EProp._EPropCoinBindSundry) 
                    {
                        loc2 = ItemsUtil.CoinIcoUrl;
                    }
                    LoaderManager.instance.load(loc2, this.onLoaded, LoaderPriority.LevelD, this.getClildPos(arg2));
                }
            }
            return;
        }

        /* internal  */onLoaded(arg1: ImageInfo): void
        {
            FlyToNavbarTool.flyToBackPack(arg1.bitmapData, arg1.extData as Object /* flash.geom.Point */);
            return;
        }

        /* internal */ /* var */_list: GList;

        /* internal */ /* var */_dataProvider: DataProvider;

        /* internal */ /* var */_dropItemInfo: DropItemInfo;

        /* internal */ /* var */_pageTool: PageTool;

        /* internal */ /* var */_pageText: TextBox;

        /* internal */ /* var */_dropItemPlayer: DropItemPlayer;
    }
